#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>

int main ( int argc, char** argv )
{
    int cpt=0,page=0,ca=0,cpt2=0,cp=0;
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    atexit(SDL_Quit);

    SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Surface* bmpp;
    SDL_Surface* bmp = SDL_LoadBMP("arriereplan.bmp");
    SDL_Surface* bmp1 = SDL_LoadBMP("curseur.bmp");
    SDL_SetColorKey(bmp1, SDL_SRCCOLORKEY, SDL_MapRGB(bmp1->format, 0, 0, 0));
    SDL_Surface* bmp2 = SDL_LoadBMP("tapis.bmp");
    SDL_Surface* bmp3 = SDL_LoadBMP("rg1.bmp");
    SDL_Surface* bmp4 = SDL_LoadBMP("rg2.bmp");
    SDL_Surface* bmp5 = SDL_LoadBMP("rg3.bmp");
    SDL_Surface* bmp6 = SDL_LoadBMP("suivant.bmp");
    SDL_Surface* bmp7 = SDL_LoadBMP("precedent.bmp");
    SDL_Surface* bmp8 = SDL_LoadBMP("suivantf.bmp");
    SDL_Surface* bmp9 = SDL_LoadBMP("precedentf.bmp");
    SDL_Surface* bmp10 = SDL_LoadBMP("musique.bmp");



    SDL_Surface* bmp11 = SDL_LoadBMP("00.bmp");

    SDL_Surface* bmp13 = SDL_LoadBMP("10.bmp");
    SDL_Surface* bmp14= SDL_LoadBMP("11.bmp");
    SDL_Surface* bmp15 = SDL_LoadBMP("20.bmp");
    SDL_Surface* bmp16 = SDL_LoadBMP("21.bmp");
    SDL_Surface* bmp17 = SDL_LoadBMP("22.bmp");
    SDL_Surface* bmp18 = SDL_LoadBMP("30.bmp");
    SDL_Surface* bmp19 = SDL_LoadBMP("31.bmp");
    SDL_Surface* bmp20 = SDL_LoadBMP("32.bmp");
    SDL_Surface* bmp21 = SDL_LoadBMP("33.bmp");
    SDL_Surface* bmp22 = SDL_LoadBMP("40.bmp");
    SDL_Surface* bmp23= SDL_LoadBMP("41.bmp");
    SDL_Surface* bmp24 = SDL_LoadBMP("42.bmp");
    SDL_Surface* bmp25 = SDL_LoadBMP("43.bmp");
    SDL_Surface* bmp26 = SDL_LoadBMP("44.bmp");
    SDL_Surface* bmp27 = SDL_LoadBMP("50.bmp");
    SDL_Surface* bmp28 = SDL_LoadBMP("51.bmp");
    SDL_Surface* bmp29 = SDL_LoadBMP("52.bmp");
    SDL_Surface* bmp30 = SDL_LoadBMP("53.bmp");
    SDL_Surface* bmp31 = SDL_LoadBMP("54.bmp");
    SDL_Surface* bmp32= SDL_LoadBMP("55.bmp");
    SDL_Surface* bmp33 = SDL_LoadBMP("60.bmp");
    SDL_Surface* bmp34 = SDL_LoadBMP("61.bmp");
    SDL_Surface* bmp35 = SDL_LoadBMP("62.bmp");
    SDL_Surface* bmp36 = SDL_LoadBMP("63.bmp");
    SDL_Surface* bmp37= SDL_LoadBMP("64.bmp");
    SDL_Surface* bmp38 = SDL_LoadBMP("65.bmp");
    SDL_Surface* bmp39 = SDL_LoadBMP("66.bmp");







    if (!bmp)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Rect dstrect;
    dstrect.x = (screen->w - bmp->w) / 2;
    dstrect.y = (screen->h - bmp->h) / 2;
    SDL_Rect dstrect1;
    dstrect1.x = 350;
    dstrect1.y = 200;
    SDL_Rect dstrect2;
    dstrect2.x = 0;
    dstrect2.y = 0;
    SDL_Rect dstrect3;
    dstrect3.x = 0;
    dstrect3.y = 0;
    SDL_Rect dstrect6;
    dstrect6.x = 413;
    dstrect6.y = 600;
    SDL_Rect dstrect7;
    dstrect7.x = 409;
    dstrect7.y = 676;
    SDL_Rect dstrect8;
    dstrect8.x = 413;
    dstrect8.y = 600;
    SDL_Rect dstrect9;
    dstrect9.x = 409;
    dstrect9.y = 676;
    SDL_Rect dstrect10;
    dstrect10.x = 0;
    dstrect10.y = 0;


    SDL_Rect dstrect11,dstrect12,dstrect13,dstrect14,dstrect15,dstrect16,dstrect17,dstrect18,dstrect19,dstrect20,dstrect21,dstrect22,dstrect23,dstrect24,dstrect25,dstrect26,dstrect27,dstrect28,dstrect29,dstrect30,dstrect31,dstrect32,dstrect33,dstrect34,dstrect35,dstrect36,dstrect37,dstrect38,dstrect39;

    dstrect11.x =70;
    dstrect11.y =140;

    dstrect13.x =70;
    dstrect13.y =190;

    dstrect14.x =70;
    dstrect14.y =240;

    dstrect15.x =70;
    dstrect15.y=290;

    dstrect16.x =70;
    dstrect16.y =340;

    dstrect17.x =70;
    dstrect17.y =390;

    dstrect18.x =70;
    dstrect18.y =440;

    dstrect19.x =70;
    dstrect19.y =490;

    dstrect20.x =70;
    dstrect20.y =540;

    dstrect21.x =70;
    dstrect21.y =590;

    dstrect22.x =70;
    dstrect22.y =640;

    dstrect23.x =70;
    dstrect23.y =690;

    dstrect24.x =70;
    dstrect24.y =740;

    dstrect25.x =70;
    dstrect25.y =790;

    dstrect26.x =688;
    dstrect26.y =140;

    dstrect27.x =688;
    dstrect27.y =190;

    dstrect28.x =688;
    dstrect28.y =240;

    dstrect29.x =688;
    dstrect29.y =290;

    dstrect30.x =688;
    dstrect30.y =340;

    dstrect31.x =688;
    dstrect31.y =390;

    dstrect32.x =688;
    dstrect32.y =440;

    dstrect33.x =688;
    dstrect33.y =490;

    dstrect34.x =688;
    dstrect34.y =540;

    dstrect35.x =688;
    dstrect35.y =590;

    dstrect36.x =688;
    dstrect36.y =640;

    dstrect37.x =688;
    dstrect37.y =690;

    dstrect38.x =688;
    dstrect38.y =740;

    dstrect39.x =688;
    dstrect39.y =790;

    bool done = false;
    while (!done)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = true;
                break;

            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                {
                    if (page==0)
                        done = true;
                    else if (page==1||page==2||page==3)
                        page=0;
                }
                else if (event.key.keysym.sym == SDLK_DOWN)
                {
                    if(page==0)
                    {
                        if (cpt==0)
                        {
                            dstrect1.y=290;
                            cpt++;
                        }
                        else if (cpt==1)
                        {
                            dstrect1.y=380;
                            cpt++;
                        }
                        else if (cpt==2)
                        {
                            dstrect1.y=570;
                            cpt++;
                        }
                    }
                    else  if (page==1)
                    {
                        if (cp==0)
                            dstrect11.y+=5;
                            if (cp==1)
                            dstrect13.y+=5;
                    }
                    else if(page==3)
                    {
                        if (cpt2==0)
                        {
                            dstrect1.y=380;
                            cpt2++;
                        }
                    }else if (page==4)
                    {if (cp==0)
                        {dstrect11.x+=5;
                        cp=1;


                        }
                        else if (cp==1)
                        {
                            dstrect13.x+=5;
                            dstrect11.x-=5;
                            cp=2;
                        }else if (cp==2)
                        {
                            dstrect14.x+=5;
                            dstrect13.x-=5;
                            cp=3;
                        }

                    else if (cp==3)
                        {
                            dstrect15.x+=5;
                            dstrect14.x-=5;
                            cp=4;
                        }
                        else if (cp==4)
                        {
                            dstrect16.x+=5;
                            dstrect15.x-=5;
                            cp=5;
                        }
                        else if (cp==5)
                        {
                            dstrect17.x+=5;
                            dstrect16.x-=5;
                            cp=6;
                        }
                        else if (cp==6)
                        {
                            dstrect18.x+=5;
                            dstrect17.x-=5;
                            cp=7;
                        }
                        else if (cp==7)
                        {
                            dstrect19.x+=5;
                            dstrect18.x-=5;
                            cp=8;
                        }
                        else if (cp==8)
                        {
                            dstrect20.x+=5;
                            dstrect19.x-=5;
                            cp=9;
                        }
                        else if (cp==9)
                        {
                            dstrect21.x+=5;
                            dstrect20.x-=5;
                            cp=10;
                        }
                        else if (cp==10)
                        {
                            dstrect22.x+=5;
                            dstrect21.x-=5;
                            cp=11;
                        }
                        else if (cp==11)
                        {
                            dstrect23.x+=5;
                            dstrect22.x-=5;
                            cp=12;
                        }
                        else if (cp==12)
                        {
                            dstrect24.x+=5;
                            dstrect23.x-=5;
                            cp=13;
                        }
                        else if (cp==13)
                        {
                            dstrect25.x+=5;
                            dstrect24.x-=5;
                            cp=14;
                        }
                }}
                else if (event.key.keysym.sym == SDLK_UP)
                {
                    if(page==0)
                    {
                        if (cpt==3)
                        {
                            dstrect1.y=380;
                            cpt--;
                        }
                        else if (cpt==2)
                        {
                            dstrect1.y=290;
                            cpt--;
                        }
                        else if (cpt==1)
                        {
                            dstrect1.y=200;
                            cpt--;
                        }
                    }else if (page==4)
                    {if (cp==1)
                        {dstrect11.x-=5;
                        cp=0;


                        }
                        else if (cp==2)
                        {
                            dstrect11.x+=5;
                            dstrect13.x-=5;
                            cp=1;
                        }else if (cp==3)
                        {
                            dstrect13.x+=5;
                            dstrect14.x-=5;
                            cp=2;
                        }
                        else if (cp==4)
                        {
                            dstrect14.x+=5;
                            dstrect15.x-=5;
                            cp=3;
                        }
                        else if (cp==5)
                        {
                            dstrect15.x+=5;
                            dstrect16.x-=5;
                            cp=4;
                        }
                        else if (cp==6)
                        {
                            dstrect16.x+=5;
                            dstrect17.x-=5;
                            cp=5;
                        }
                        else if (cp==7)
                        {
                            dstrect17.x+=5;
                            dstrect18.x-=5;
                            cp=6;
                        }
                        else if (cp==8)
                        {
                            dstrect18.x+=5;
                            dstrect19.x-=5;
                            cp=7;
                        }
                        else if (cp==9)
                        {
                            dstrect19.x+=5;
                            dstrect20.x-=5;
                            cp=8;
                        }
                        else if (cp==10)
                        {
                            dstrect20.x+=5;
                            dstrect21.x-=5;
                            cp=9;
                        }
                        else if (cp==11)
                        {
                            dstrect21.x+=5;
                            dstrect22.x-=5;
                            cp=10;
                        }
                        else if (cp==12)
                        {
                            dstrect22.x+=5;
                            dstrect23.x-=5;
                            cp=11;
                        }
                        else if (cp==13)
                        {
                            dstrect23.x+=5;
                            dstrect24.x-=5;
                            cp=12;
                        }
                         else if (cp==14)
                        {
                            dstrect23.x+=5;
                            dstrect24.x-=5;
                            cp=13;
                        }


                        }
                    else if (page==1)
                    {
                        if (cp==0)
                            dstrect11.y-=5;
                           else  if (cp==1)
                            dstrect13.y-=5;
                    }
                    else if(page==3)
                    {
                        if (cpt2==2)
                        {
                            dstrect1.y=380;
                            cpt2--;
                        }
                        else if (cpt2==1)
                        {
                            dstrect1.y=290;
                            cpt2--;
                        }

                    }
                }
                else if (event.key.keysym.sym == SDLK_RETURN)
                {
                    if (page==0&&cpt==0)
                        page=4;

                    else if (page==0&&cpt==1)
                    {
                        page=3;
                        dstrect1.y=275;
                        dstrect1.x=325;
                    }
                    else if (page==0&&cpt==2)
                        page=2;
                    else if(page==0&&cpt==3)
                        done=true;
                    else if(page==3 && dstrect1.y==570)
                        page=0;
                    else if (page==4)
                        page=1;
                    else if (page==1)
                        page=4;
                }
                else if (event.key.keysym.sym == SDLK_RIGHT)
                {
                    if(ca==0)
                    {
                        bmpp=bmp3;
                        bmp3=bmp4;
                        bmp4=bmpp;
                        ca++;
                    }
                    else if(ca==1)
                    {
                        bmpp=bmp3;
                        bmp3=bmp5;
                        bmp5=bmpp;
                        ca++;
                    }
                    if (page==1)
                    {
                        if (cp==0)
                            dstrect11.x+=5;
                           else if (cp==1)
                            dstrect13.x+=5;
                    }
                }
                else if (event.key.keysym.sym == SDLK_LEFT)
                {
                    if(ca==2)
                    {
                        bmpp=bmp5;
                        bmp5=bmp3;
                        bmp3=bmpp;
                        ca--;
                    }
                    else if(ca==1)
                    {
                        bmpp=bmp4;
                        bmp4=bmp3;
                        bmp3=bmpp;
                        ca--;
                    }
                    else if (page==1)
                    {
                        if ( cp==0)
                            dstrect11.x-=5;
                         else   if (cp==1)
                        dstrect13.x-=5;
                    }

                }

                break;
            }
            }
            if (page==0)
            {
                SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
                SDL_BlitSurface(bmp, 0, screen, &dstrect);
                SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
            }
            else if (page==1)
            {
                SDL_Surface* screen = SDL_SetVideoMode(800, 1024, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                SDL_BlitSurface(bmp2, 0, screen, &dstrect2);
                SDL_BlitSurface(bmp11, 0, screen, &dstrect11);
                SDL_BlitSurface(bmp13, 0, screen, &dstrect13);
                SDL_BlitSurface(bmp14, 0, screen, &dstrect14);
                SDL_BlitSurface(bmp15, 0, screen, &dstrect15);
                SDL_BlitSurface(bmp16, 0, screen, &dstrect16);
                SDL_BlitSurface(bmp17, 0, screen, &dstrect17);
                SDL_BlitSurface(bmp18, 0, screen, &dstrect18);
                SDL_BlitSurface(bmp19, 0, screen, &dstrect19);
                SDL_BlitSurface(bmp20, 0, screen, &dstrect20);
                SDL_BlitSurface(bmp21, 0, screen, &dstrect21);
                SDL_BlitSurface(bmp22, 0, screen, &dstrect22);
                SDL_BlitSurface(bmp23, 0, screen, &dstrect23);
                SDL_BlitSurface(bmp24, 0, screen, &dstrect24);
                SDL_BlitSurface(bmp25, 0, screen, &dstrect25);
                SDL_BlitSurface(bmp26, 0, screen, &dstrect26);
                SDL_BlitSurface(bmp27, 0, screen, &dstrect27);
                SDL_BlitSurface(bmp28, 0, screen, &dstrect28);
                SDL_BlitSurface(bmp29, 0, screen, &dstrect29);
                SDL_BlitSurface(bmp30, 0, screen, &dstrect30);
                SDL_BlitSurface(bmp31, 0, screen, &dstrect31);
                SDL_BlitSurface(bmp32, 0, screen, &dstrect32);
                SDL_BlitSurface(bmp33, 0, screen, &dstrect33);
                SDL_BlitSurface(bmp34, 0, screen, &dstrect34);
                SDL_BlitSurface(bmp35, 0, screen, &dstrect35);
                SDL_BlitSurface(bmp36, 0, screen, &dstrect36);
                SDL_BlitSurface(bmp37, 0, screen, &dstrect37);
                SDL_BlitSurface(bmp38, 0, screen, &dstrect38);
                SDL_BlitSurface(bmp39, 0, screen, &dstrect39);
            }
            else if (page==2)
            {
                if (ca==1)
                {
                    SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                    SDL_BlitSurface(bmp3, 0, screen, &dstrect3);
                    SDL_BlitSurface(bmp6, 0, screen, &dstrect6);
                    SDL_BlitSurface(bmp7, 0, screen, &dstrect7);
                }
                else if(ca==0)
                {
                    SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                    SDL_BlitSurface(bmp3, 0, screen, &dstrect3);
                    SDL_BlitSurface(bmp6, 0, screen, &dstrect6);
                    SDL_BlitSurface(bmp9, 0, screen, &dstrect9);
                }
                else if(ca==2)
                {
                    SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                    SDL_BlitSurface(bmp3, 0, screen, &dstrect3);
                    SDL_BlitSurface(bmp6, 0, screen, &dstrect6);
                    SDL_BlitSurface(bmp7, 0, screen, &dstrect7);
                    SDL_BlitSurface(bmp8, 0, screen, &dstrect8);
                }
            }
            else if(page==3)
            {
                SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                SDL_BlitSurface(bmp10, 0, screen, &dstrect10);
                SDL_BlitSurface(bmp1, 0, screen, &dstrect1);
            }
            else if (page==4)
            {
                SDL_Surface* screen = SDL_SetVideoMode(800, 1024, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
                SDL_BlitSurface(bmp2, 0, screen, &dstrect2);
                SDL_BlitSurface(bmp11, 0, screen, &dstrect11);
                SDL_BlitSurface(bmp13, 0, screen, &dstrect13);
                SDL_BlitSurface(bmp14, 0, screen, &dstrect14);
                SDL_BlitSurface(bmp15, 0, screen, &dstrect15);
                SDL_BlitSurface(bmp16, 0, screen, &dstrect16);
                SDL_BlitSurface(bmp17, 0, screen, &dstrect17);
                SDL_BlitSurface(bmp18, 0, screen, &dstrect18);
                SDL_BlitSurface(bmp19, 0, screen, &dstrect19);
                SDL_BlitSurface(bmp20, 0, screen, &dstrect20);
                SDL_BlitSurface(bmp21, 0, screen, &dstrect21);
                SDL_BlitSurface(bmp22, 0, screen, &dstrect22);
                SDL_BlitSurface(bmp23, 0, screen, &dstrect23);
                SDL_BlitSurface(bmp24, 0, screen, &dstrect24);
                SDL_BlitSurface(bmp25, 0, screen, &dstrect25);
                SDL_BlitSurface(bmp26, 0, screen, &dstrect26);
                SDL_BlitSurface(bmp27, 0, screen, &dstrect27);
                SDL_BlitSurface(bmp28, 0, screen, &dstrect28);
                SDL_BlitSurface(bmp29, 0, screen, &dstrect29);
                SDL_BlitSurface(bmp30, 0, screen, &dstrect30);
                SDL_BlitSurface(bmp31, 0, screen, &dstrect31);
                SDL_BlitSurface(bmp32, 0, screen, &dstrect32);
                SDL_BlitSurface(bmp33, 0, screen, &dstrect33);
                SDL_BlitSurface(bmp34, 0, screen, &dstrect34);
                SDL_BlitSurface(bmp35, 0, screen, &dstrect35);
                SDL_BlitSurface(bmp36, 0, screen, &dstrect36);
                SDL_BlitSurface(bmp37, 0, screen, &dstrect37);
                SDL_BlitSurface(bmp38, 0, screen, &dstrect38);
                SDL_BlitSurface(bmp39, 0, screen, &dstrect39);
            }
            SDL_Flip(screen);
        }
    }
    SDL_FreeSurface(bmp);
    printf("Exited cleanly\n");
    return 0;
}
